﻿// -----------------------------------------------------------------------
// <copyright file="CourseRepository.cs" company="AGR">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace AGR.Data.Repository.Contract
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using AGR.Model;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class CourseRepository : AbstractRepository<Course>
    {
        public CourseRepository(ContextHelper contextHelper)
        {
            base._contextHelper = contextHelper;
            base._context = _contextHelper.GetContext();
        }

        public override IQueryable<Course> GetAll()
        {
            var result = (from x in base._context.Courses
                          select new Course
                          {
                              Id = x.Id,
                              CourseCode = x.CourseCode,
                              CourseName = x.CourseName,
                              AccreditationNumber = x.AccreditationNumber,
                              Category = x.Category,
                              CertificateType = x.CertificateType,
                              DateCreated = x.DateCreated,
                              UserCreated = x.UserCreated,
                              DateModified = x.DateModified,
                              UserModified = x.UserModified
                          });

            return result;
        }

        public override void Add(Course entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Course entityToAdd = new Context.Course
            {
                CourseCode = entity.CourseCode,
                CourseName = entity.CourseName,
                AccreditationNumber = entity.AccreditationNumber,
                Category = entity.Category,
                CertificateType = entity.CertificateType,
                DateCreated = DateTime.Now,
                UserCreated = entity.UserCreated,
                DateModified = null,
                UserModified = null
            };

            base._context.Courses.InsertOnSubmit(entityToAdd);
        }

        public override void Update(Course entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Course entityToUpdate = base._context.Courses.Single(x => x.CourseCode.Equals(entity.CourseCode));

            entityToUpdate.CourseCode = entity.CourseCode;
            entityToUpdate.CourseName = entity.CourseName;
            entityToUpdate.AccreditationNumber = entity.AccreditationNumber;
            entityToUpdate.Category = entity.Category;
            entityToUpdate.CertificateType = entity.CertificateType;
            entityToUpdate.DateModified = DateTime.Now;
            entityToUpdate.UserModified = entity.UserModified;
        }

        public override void Delete(Course entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Course entityToDelete = base._context.Courses.Single(x => x.CourseCode.Equals(entity.CourseCode));

            base._context.Courses.DeleteOnSubmit(entityToDelete);
        }
    }
}
